package de.fhd.pms.dao;

import static org.junit.Assert.assertEquals;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.validation.constraints.AssertFalse;

import org.junit.Before;
import org.junit.Test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import de.fhd.pms.dao.UserDao;
import de.fhd.pms.model.User;
/**
 * A Test class (Junit) for {@link UserDao} to find out if all Methods defined in UserDao are working fine.
 * This class uses a special test-database "hitchtest".
 */
public class UserDaoTest extends AbstractDataAccessTest {

	private static final Log log = LogFactory.getLog(UserDaoTest.class);

	private static final String[] configFiles = new String[] { "test-application-context.xml" };

	private ApplicationContext ctx = null;
	private UserDao userDao;
	private User user;
	private Date birthdate;

	private String tableName = "users";
	private String[] tables = { tableName };

	/**Set UserDao
	 @param userDao
	  */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	/**Setup Configuration
	  */
	@Before
	public void onSetUp() {

		user = new User();

		Date date = new Date();

		birthdate = date;

		user.setEnabled(1);
		user.setBirthdate(birthdate);
		user.setGender(1);
		user.setEmail("test@test.lv");
		user.setUsername("andreas");
		user.setPassword("qwER12!@");
		user.setPasswordConfirm("qwER12!@");
		user.setName("Andreas");
		user.setLastname("Janow");

		ctx = new ClassPathXmlApplicationContext(configFiles);
		log.debug("ctx: " + ctx);

	}
	/**
	 * A Test to find out if the {@link User} can be created in depentent on {@link User} and {@link Action}.
	 */
	@Test
	public void testFindById() {

		userDao.save(user);
		List<User> alle = userDao.findAll();
		if (alle == null) {
			Assert.fail();
		}
		for (User u : alle) {
			int id = u.getId();
			User found = userDao.findByUserId(id);
			assertEquals(u.getUsername(), found.getUsername());
		}
		// delete all rows from db table
		userDao.delete(user);
	}
	/**
	 * A Test to find out if the {@link User} was created.
	 */
	@Test
	public void testFindByUsername(){
		userDao.save(user);
		String testUsername = "andreas";
		User testUser = userDao.findByUsername(testUsername);
		assertNotNull(testUser);
		userDao.delete(user);
	}
	
	/**
	 * A Test to find out if all Users can be found in the test-database.
	 */
	@Test
	public void testFindAll() {
		// already tested by testFindById()
		testFindById();
	}
	/**
	 * A Test to find out if a {@link User} can be created in the test-database.
	 */
	@Test
	public void testCreate() {
		// already tested by testFindById()
		testFindById();
	}

}
